package com.tedu.service.impl;

import com.tedu.mapper.ArticleMapper;
import com.tedu.pojo.Article;
import com.tedu.pojo.ResultVO;
import com.tedu.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author：毕龙
 * Date: 2023/12/23 9:42
 */
@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    public ArticleServiceImpl() {
        log.debug("创建业务层ArticleServiceImpl对象");
    }

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public ResultVO<Article> listArticle() {
        List<Article> list = articleMapper.listArticle();
        if (list.size() == 0) {
            return ResultVO.fail("没有查询到文章内容");
        }
        return ResultVO.success(list, "查询文章内容成功");
    }

    @Override
    @Transactional
    public ResultVO<String> insertArticle(Article article) {

        log.debug("开始添加文章内容：{}", article);
        //通过文章标题获取文章信息
        List<Article> list = articleMapper.getArticleByTitle(article.getTitle());
        try {
            //遍历所有的文章信息
            list.forEach(article1 -> {
                //判断文章的内容和输入的文章内容是否一致
                if (article1.getContent().equals(article.getContent())) {
                    throw new RuntimeException("该文章内容已存在");
                }
            });
        }catch (Exception e){
            return ResultVO.fail(e.getMessage());
        }

        if(article.getTitle() == null){
            return ResultVO.fail("为证标题为空");
        }

        int i = articleMapper.insertArticle(article);

        if (i == 0) {
            log.debug("添加文章失败");
            return ResultVO.fail("添加失败");
        }
        log.debug("添加文章内容完成：{}", article);
        return ResultVO.success(article, "添加成功");

    }

    @Override
    public ResultVO<Article> getArticleById(Integer id) {

        log.debug("开始进行根据id查询文章信息");
        Article article = articleMapper.getArticleById(id);
        log.debug("查询文章信息是：{}",article);
        if(article == null){
          return  ResultVO.fail("该文章信息不存在");
        }

        return ResultVO.success(article,"查询成功");
    }

    @Override
    public ResultVO<Article> updateArticleById(Article article) {

        log.debug("进行文章内容的修改");
        //查询id的文章是否存在
        try {
            Article odlArticle = articleMapper.getArticleById(article.getId());
            if(odlArticle == null){
                throw new RuntimeException("不存在该文章");
            }
        }catch (Exception e){
            return  ResultVO.fail(e.getMessage());
        }

        //修改完成的内容是否跟已存在的数据的内容相同
        //通过文章标题获取文章信息
        List<Article> list = articleMapper.getArticleByTitle(article.getTitle());
        try {
            //遍历所有的文章信息
            list.forEach(article1 -> {
                //判断文章的内容和输入的文章内容是否一致
                if (article1.getContent().equals(article.getContent())) {
                    throw new RuntimeException("该文章内容已存在");
                }
            });
        }catch (Exception e){
            return ResultVO.fail(e.getMessage());
        }

        //进行修改的功能
        int i = articleMapper.updateArticle(article);
        if(i == 0){
            return  ResultVO.fail("修改文章内容失败");
        }

        return  ResultVO.success(article,"修改该数据成功！");
    }

    @Override
    public ResultVO<Article> deleteArticleById(Integer id) {
        Article odlArticle = articleMapper.getArticleById(id);
        //查询id的文章是否存在
        try {

            if(odlArticle == null){
                throw new RuntimeException("不存在该文章");
            }
        }catch (Exception e){
            return  ResultVO.fail(e.getMessage());
        }

        int i = articleMapper.deleteArticleById(id);
        if(i == 0){
            return  ResultVO.fail("删除文章内容失败");
        }

        return  ResultVO.success(odlArticle,"删除该数据成功！");
    }
}
